package com.appwoo.txtws.thread;

import android.content.Context;
import android.os.AsyncTask;
import android.widget.ProgressBar;
import com.appwoo.txtws.activity.BookstoreActivity;
import com.appwoo.txtws.bean.BookBean;
import com.appwoo.txtws.bean.BookSubjectBean;
import com.appwoo.txtws.component.BookHelper;
import com.appwoo.txtws.component.DBHelper;
import com.appwoo.txtws.component.Global;
import com.appwoo.txtws.component.Logger;
import com.appwoo.txtws.service.BookService;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.ArrayList;
import org.apache.http.conn.ConnectTimeoutException;

public class BookStoreDownloadTask extends AsyncTask<String, Integer, String>
{
  public static final String RESULT_ERROR_BEFORE_CANCEL = "5";
  public static final String RESULT_ERROR_CANCEL = "4";
  public static final String RESULT_ERROR_CREATE_FILE = "6";
  public static final String RESULT_ERROR_FILE_NOT_FOUND = "2";
  public static final String RESULT_ERROR_SERVICE_FILE = "3";
  public static final String RESULT_MISSING_PARAMETER = "0";
  public static final String RESULT_NOT_NETWORK = "1";
  public static final String RESULT_SUCCESS = "20";
  private BookBean book;
  private BookHelper bookHelper;
  private BookService bookService;
  private BookStoreDownloadComplete complete;
  private String downloadPath;
  private int fileCount;
  private int fileSize;
  private String grade;
  private boolean isCancelDownload;
  private boolean isRun;
  private String localDirPath;
  private DBHelper mDBHelper;
  private Object obj;
  private BookStoreDownloadPre pre;
  private ProgressBar progress;
  private int readedSize;
  private String subject;
  private int type;

  public BookStoreDownloadTask(Context paramContext, BookBean paramBookBean, ProgressBar paramProgressBar, BookStoreDownloadPre paramBookStoreDownloadPre, BookStoreDownloadComplete paramBookStoreDownloadComplete, String paramString1, String paramString2, int paramInt, String paramString3, String paramString4, Object paramObject)
  {
    this.progress = paramProgressBar;
    this.complete = paramBookStoreDownloadComplete;
    this.pre = paramBookStoreDownloadPre;
    this.book = paramBookBean;
    this.downloadPath = paramString1;
    this.localDirPath = paramString2;
    this.bookHelper = new BookHelper();
    this.type = paramInt;
    this.subject = paramString3;
    this.obj = paramObject;
    this.grade = paramString4;
    this.mDBHelper = new DBHelper(paramContext);
    this.bookService = new BookService(paramContext);
  }

  // ERROR //
  private String download(String paramString1, String paramString2)
  {
    // Byte code:
    //   0: aconst_null
    //   1: astore_3
    //   2: aconst_null
    //   3: astore 4
    //   5: aconst_null
    //   6: astore 5
    //   8: new 110	java/net/URL
    //   11: dup
    //   12: aload_1
    //   13: invokespecial 113	java/net/URL:<init>	(Ljava/lang/String;)V
    //   16: invokevirtual 117	java/net/URL:openConnection	()Ljava/net/URLConnection;
    //   19: checkcast 119	java/net/HttpURLConnection
    //   22: astore 4
    //   24: aload 4
    //   26: sipush 5000
    //   29: invokevirtual 123	java/net/HttpURLConnection:setConnectTimeout	(I)V
    //   32: aload 4
    //   34: invokevirtual 127	java/net/HttpURLConnection:getInputStream	()Ljava/io/InputStream;
    //   37: astore 5
    //   39: aload 4
    //   41: invokevirtual 131	java/net/HttpURLConnection:getResponseCode	()I
    //   44: istore 18
    //   46: aconst_null
    //   47: astore_3
    //   48: iload 18
    //   50: sipush 200
    //   53: if_icmpne +233 -> 286
    //   56: aload_0
    //   57: aload 4
    //   59: invokevirtual 134	java/net/HttpURLConnection:getContentLength	()I
    //   62: putfield 136	com/appwoo/txtws/thread/BookStoreDownloadTask:fileSize	I
    //   65: aload_0
    //   66: getfield 136	com/appwoo/txtws/thread/BookStoreDownloadTask:fileSize	I
    //   69: istore 19
    //   71: iload 19
    //   73: ifgt +58 -> 131
    //   76: iconst_0
    //   77: ifeq +11 -> 88
    //   80: aconst_null
    //   81: invokevirtual 141	java/io/FileOutputStream:flush	()V
    //   84: aconst_null
    //   85: invokevirtual 144	java/io/FileOutputStream:close	()V
    //   88: aload 5
    //   90: ifnull +8 -> 98
    //   93: aload 5
    //   95: invokevirtual 147	java/io/InputStream:close	()V
    //   98: aload 4
    //   100: ifnull +8 -> 108
    //   103: aload 4
    //   105: invokevirtual 150	java/net/HttpURLConnection:disconnect	()V
    //   108: ldc 21
    //   110: areturn
    //   111: astore 26
    //   113: aload 26
    //   115: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   118: goto -30 -> 88
    //   121: astore 25
    //   123: aload 25
    //   125: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   128: goto -30 -> 98
    //   131: new 138	java/io/FileOutputStream
    //   134: dup
    //   135: new 155	java/io/File
    //   138: dup
    //   139: aload_2
    //   140: invokespecial 156	java/io/File:<init>	(Ljava/lang/String;)V
    //   143: invokespecial 159	java/io/FileOutputStream:<init>	(Ljava/io/File;)V
    //   146: astore 20
    //   148: sipush 1024
    //   151: newarray byte
    //   153: astore 21
    //   155: aload 5
    //   157: aload 21
    //   159: invokevirtual 163	java/io/InputStream:read	([B)I
    //   162: istore 22
    //   164: iload 22
    //   166: iconst_m1
    //   167: if_icmpgt +41 -> 208
    //   170: aload 20
    //   172: ifnull +13 -> 185
    //   175: aload 20
    //   177: invokevirtual 141	java/io/FileOutputStream:flush	()V
    //   180: aload 20
    //   182: invokevirtual 144	java/io/FileOutputStream:close	()V
    //   185: aload 5
    //   187: ifnull +8 -> 195
    //   190: aload 5
    //   192: invokevirtual 147	java/io/InputStream:close	()V
    //   195: aload 4
    //   197: ifnull +8 -> 205
    //   200: aload 4
    //   202: invokevirtual 150	java/net/HttpURLConnection:disconnect	()V
    //   205: ldc 30
    //   207: areturn
    //   208: aload 20
    //   210: aload 21
    //   212: iconst_0
    //   213: iload 22
    //   215: invokevirtual 167	java/io/FileOutputStream:write	([BII)V
    //   218: goto -63 -> 155
    //   221: astore 6
    //   223: aload 20
    //   225: astore_3
    //   226: aload 6
    //   228: invokevirtual 168	java/net/MalformedURLException:printStackTrace	()V
    //   231: aload_3
    //   232: ifnull +11 -> 243
    //   235: aload_3
    //   236: invokevirtual 141	java/io/FileOutputStream:flush	()V
    //   239: aload_3
    //   240: invokevirtual 144	java/io/FileOutputStream:close	()V
    //   243: aload 5
    //   245: ifnull +8 -> 253
    //   248: aload 5
    //   250: invokevirtual 147	java/io/InputStream:close	()V
    //   253: aload 4
    //   255: ifnull +8 -> 263
    //   258: aload 4
    //   260: invokevirtual 150	java/net/HttpURLConnection:disconnect	()V
    //   263: ldc 18
    //   265: areturn
    //   266: astore 24
    //   268: aload 24
    //   270: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   273: goto -88 -> 185
    //   276: astore 23
    //   278: aload 23
    //   280: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   283: goto -88 -> 195
    //   286: iload 18
    //   288: sipush 404
    //   291: if_icmpne +259 -> 550
    //   294: iconst_0
    //   295: ifeq +11 -> 306
    //   298: aconst_null
    //   299: invokevirtual 141	java/io/FileOutputStream:flush	()V
    //   302: aconst_null
    //   303: invokevirtual 144	java/io/FileOutputStream:close	()V
    //   306: aload 5
    //   308: ifnull +8 -> 316
    //   311: aload 5
    //   313: invokevirtual 147	java/io/InputStream:close	()V
    //   316: aload 4
    //   318: ifnull +8 -> 326
    //   321: aload 4
    //   323: invokevirtual 150	java/net/HttpURLConnection:disconnect	()V
    //   326: ldc 18
    //   328: areturn
    //   329: astore 30
    //   331: aload 30
    //   333: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   336: goto -30 -> 306
    //   339: astore 29
    //   341: aload 29
    //   343: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   346: goto -30 -> 316
    //   349: astore 11
    //   351: aload 11
    //   353: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   356: goto -113 -> 243
    //   359: astore 10
    //   361: aload 10
    //   363: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   366: goto -113 -> 253
    //   369: astore 15
    //   371: aload 15
    //   373: invokevirtual 169	java/io/FileNotFoundException:printStackTrace	()V
    //   376: aload_3
    //   377: ifnull +11 -> 388
    //   380: aload_3
    //   381: invokevirtual 141	java/io/FileOutputStream:flush	()V
    //   384: aload_3
    //   385: invokevirtual 144	java/io/FileOutputStream:close	()V
    //   388: aload 5
    //   390: ifnull +8 -> 398
    //   393: aload 5
    //   395: invokevirtual 147	java/io/InputStream:close	()V
    //   398: aload 4
    //   400: ifnull +8 -> 408
    //   403: aload 4
    //   405: invokevirtual 150	java/net/HttpURLConnection:disconnect	()V
    //   408: ldc 18
    //   410: areturn
    //   411: astore 17
    //   413: aload 17
    //   415: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   418: goto -30 -> 388
    //   421: astore 16
    //   423: aload 16
    //   425: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   428: goto -30 -> 398
    //   431: astore 12
    //   433: aload 12
    //   435: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   438: aload_3
    //   439: ifnull +11 -> 450
    //   442: aload_3
    //   443: invokevirtual 141	java/io/FileOutputStream:flush	()V
    //   446: aload_3
    //   447: invokevirtual 144	java/io/FileOutputStream:close	()V
    //   450: aload 5
    //   452: ifnull +8 -> 460
    //   455: aload 5
    //   457: invokevirtual 147	java/io/InputStream:close	()V
    //   460: aload 4
    //   462: ifnull +8 -> 470
    //   465: aload 4
    //   467: invokevirtual 150	java/net/HttpURLConnection:disconnect	()V
    //   470: ldc 27
    //   472: areturn
    //   473: astore 14
    //   475: aload 14
    //   477: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   480: goto -30 -> 450
    //   483: astore 13
    //   485: aload 13
    //   487: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   490: goto -30 -> 460
    //   493: astore 7
    //   495: aload_3
    //   496: ifnull +11 -> 507
    //   499: aload_3
    //   500: invokevirtual 141	java/io/FileOutputStream:flush	()V
    //   503: aload_3
    //   504: invokevirtual 144	java/io/FileOutputStream:close	()V
    //   507: aload 5
    //   509: ifnull +8 -> 517
    //   512: aload 5
    //   514: invokevirtual 147	java/io/InputStream:close	()V
    //   517: aload 4
    //   519: ifnull +8 -> 527
    //   522: aload 4
    //   524: invokevirtual 150	java/net/HttpURLConnection:disconnect	()V
    //   527: aload 7
    //   529: athrow
    //   530: astore 9
    //   532: aload 9
    //   534: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   537: goto -30 -> 507
    //   540: astore 8
    //   542: aload 8
    //   544: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   547: goto -30 -> 517
    //   550: iconst_0
    //   551: ifeq +11 -> 562
    //   554: aconst_null
    //   555: invokevirtual 141	java/io/FileOutputStream:flush	()V
    //   558: aconst_null
    //   559: invokevirtual 144	java/io/FileOutputStream:close	()V
    //   562: aload 5
    //   564: ifnull +8 -> 572
    //   567: aload 5
    //   569: invokevirtual 147	java/io/InputStream:close	()V
    //   572: aload 4
    //   574: ifnull -104 -> 470
    //   577: aload 4
    //   579: invokevirtual 150	java/net/HttpURLConnection:disconnect	()V
    //   582: goto -112 -> 470
    //   585: astore 28
    //   587: aload 28
    //   589: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   592: goto -30 -> 562
    //   595: astore 27
    //   597: aload 27
    //   599: invokevirtual 153	java/io/IOException:printStackTrace	()V
    //   602: goto -30 -> 572
    //   605: astore 7
    //   607: aload 20
    //   609: astore_3
    //   610: goto -115 -> 495
    //   613: astore 12
    //   615: aload 20
    //   617: astore_3
    //   618: goto -185 -> 433
    //   621: astore 15
    //   623: aload 20
    //   625: astore_3
    //   626: goto -255 -> 371
    //   629: astore 6
    //   631: aconst_null
    //   632: astore_3
    //   633: goto -407 -> 226
    //
    // Exception table:
    //   from	to	target	type
    //   80	88	111	java/io/IOException
    //   93	98	121	java/io/IOException
    //   148	155	221	java/net/MalformedURLException
    //   155	164	221	java/net/MalformedURLException
    //   208	218	221	java/net/MalformedURLException
    //   175	185	266	java/io/IOException
    //   190	195	276	java/io/IOException
    //   298	306	329	java/io/IOException
    //   311	316	339	java/io/IOException
    //   235	243	349	java/io/IOException
    //   248	253	359	java/io/IOException
    //   8	46	369	java/io/FileNotFoundException
    //   56	71	369	java/io/FileNotFoundException
    //   131	148	369	java/io/FileNotFoundException
    //   380	388	411	java/io/IOException
    //   393	398	421	java/io/IOException
    //   8	46	431	java/io/IOException
    //   56	71	431	java/io/IOException
    //   131	148	431	java/io/IOException
    //   442	450	473	java/io/IOException
    //   455	460	483	java/io/IOException
    //   8	46	493	finally
    //   56	71	493	finally
    //   131	148	493	finally
    //   226	231	493	finally
    //   371	376	493	finally
    //   433	438	493	finally
    //   499	507	530	java/io/IOException
    //   512	517	540	java/io/IOException
    //   554	562	585	java/io/IOException
    //   567	572	595	java/io/IOException
    //   148	155	605	finally
    //   155	164	605	finally
    //   208	218	605	finally
    //   148	155	613	java/io/IOException
    //   155	164	613	java/io/IOException
    //   208	218	613	java/io/IOException
    //   148	155	621	java/io/FileNotFoundException
    //   155	164	621	java/io/FileNotFoundException
    //   208	218	621	java/io/FileNotFoundException
    //   8	46	629	java/net/MalformedURLException
    //   56	71	629	java/net/MalformedURLException
    //   131	148	629	java/net/MalformedURLException
  }

  private void updateBookSubject(BookSubjectBean paramBookSubjectBean, int paramInt)
  {
    String str1 = paramBookSubjectBean.getAscription();
    String str2 = "@" + paramInt + "@";
    if ((str1 != null) && (!"".equals(str1)))
    {
      if (str1.indexOf(str2) == -1)
      {
        paramBookSubjectBean.setAscription(str1 + str2);
        this.mDBHelper.updateBookSubjectBean(paramBookSubjectBean);
      }
      return;
    }
    paramBookSubjectBean.setAscription(str2);
    this.mDBHelper.updateBookSubjectBean(paramBookSubjectBean);
  }

  protected String doInBackground(String[] paramArrayOfString)
  {
    if (!this.isCancelDownload)
    {
      String str1;
      if ((this.downloadPath == null) || ("".equals(this.downloadPath)))
      {
        str1 = "0";
        return str1;
      }
      this.downloadPath = this.downloadPath.replace("\\", "/");
      if ((this.localDirPath == null) || ("".equals(this.localDirPath)))
        return "0";
      if (this.downloadPath.endsWith("/"));
      String str3;
      for (String str2 = this.downloadPath.substring(0, -1 + this.downloadPath.length()); ; str2 = this.downloadPath)
      {
        str3 = str2.substring(1 + str2.lastIndexOf("/"));
        if (!this.isCancelDownload)
          break;
        return "4";
      }
      ArrayList localArrayList1;
      ArrayList localArrayList2;
      try
      {
        localArrayList1 = this.bookHelper.getSignleBookDownloadFileList(this.downloadPath);
        boolean bool = this.isCancelDownload;
        if (bool)
          return "4";
        localArrayList2 = this.bookService.getBookImageBeanList(Global.USERNAME, str3);
        if (this.isCancelDownload)
          return "4";
      }
      catch (ConnectTimeoutException localConnectTimeoutException)
      {
        localConnectTimeoutException.printStackTrace();
        return "1";
      }
      catch (FileNotFoundException localFileNotFoundException)
      {
        localFileNotFoundException.printStackTrace();
        return "2";
      }
      catch (IOException localIOException)
      {
        localIOException.printStackTrace();
        return "1";
      }
      if ((localArrayList1 == null) || (localArrayList1.isEmpty()))
        return "2";
      localArrayList1.addAll(localArrayList2);
      this.fileCount = localArrayList1.size();
      if (this.progress != null)
        this.progress.setMax(this.fileCount);
      if (!this.localDirPath.endsWith("/"))
        this.localDirPath += "/";
      int i = 0;
      label303: ArrayList localArrayList3;
      int j;
      label325: Object localObject;
      if (i >= this.fileCount)
      {
        localArrayList3 = this.mDBHelper.getBookSubjectBeanList(-1);
        j = 0;
        int k = localArrayList3.size();
        localObject = null;
        if (j < k)
          break label656;
        label342: if (localObject == null)
          localObject = (BookSubjectBean)localArrayList3.get(-1 + localArrayList3.size());
        this.book.setIsRead(0);
        this.book.setSubjectId(((BookSubjectBean)localObject).getSubjectId());
        if (this.type != 0)
          break label705;
        this.book.setBookType(this.type);
        updateBookSubject((BookSubjectBean)localObject, this.type);
      }
      while (true)
      {
        this.book.setDownloadTime(System.currentTimeMillis());
        this.book.setLocalPath(Global.BOOK_PATH + this.book.getName());
        this.mDBHelper.updateBookBean(this.book);
        return "20";
        String str4 = ((String)localArrayList1.get(i)).replace(this.downloadPath, "");
        File localFile = new File(this.localDirPath + str4);
        if (localFile != null)
        {
          if (this.isCancelDownload)
            return "4";
          if (!localFile.getParentFile().exists())
            localFile.getParentFile().mkdirs();
          str1 = download(this.bookHelper.getEncodeURL(this.downloadPath + str4), localFile.getPath());
          if (!"20".equals(str1))
            break;
        }
        this.readedSize = i;
        if ((i % 3 == 0) || (i == -1 + this.fileCount))
        {
          Integer[] arrayOfInteger = new Integer[1];
          arrayOfInteger[0] = Integer.valueOf(i);
          publishProgress(arrayOfInteger);
        }
        i++;
        break label303;
        label656: BookSubjectBean localBookSubjectBean = (BookSubjectBean)localArrayList3.get(j);
        if ((localBookSubjectBean != null) && (this.book.getName().indexOf(localBookSubjectBean.getName()) > -1))
        {
          localObject = localBookSubjectBean;
          break label342;
        }
        j++;
        break label325;
        label705: if (this.type == 1)
        {
          this.book.setBookType(Integer.valueOf(this.grade).intValue());
          updateBookSubject((BookSubjectBean)localObject, this.book.getBookType());
        }
      }
    }
    return "5";
  }

  public BookBean getBook()
  {
    return this.book;
  }

  public boolean isRun()
  {
    return this.isRun;
  }

  protected void onPostExecute(String paramString)
  {
    super.onPostExecute(paramString);
    BookstoreActivity.setDownloadTaskIsRun(false);
    if (this.complete != null)
      this.complete.complete(paramString, this.type, this.subject);
  }

  protected void onPreExecute()
  {
    super.onPreExecute();
    setRun(true);
    BookstoreActivity.setDownloadTaskIsRun(true);
    if (this.pre != null)
      this.pre.onPreExecute(this.obj);
  }

  protected void onProgressUpdate(Integer[] paramArrayOfInteger)
  {
    super.onProgressUpdate(paramArrayOfInteger);
    if (this.progress != null)
    {
      System.out.println("下载进度：" + paramArrayOfInteger[0]);
      this.progress.setProgress(paramArrayOfInteger[0].intValue());
      return;
    }
    Logger.e("onProgressUpdate", "onProgressUpdate中的进度条为空");
  }

  public void recoverProgressBar()
  {
    this.progress = null;
  }

  public void setBookStoreDownloadComplete(BookStoreDownloadComplete paramBookStoreDownloadComplete)
  {
    this.complete = paramBookStoreDownloadComplete;
  }

  public void setObj(Object paramObject)
  {
    this.obj = paramObject;
  }

  public void setPre(BookStoreDownloadPre paramBookStoreDownloadPre)
  {
    this.pre = paramBookStoreDownloadPre;
  }

  public void setProgressBar(ProgressBar paramProgressBar)
  {
    this.progress = paramProgressBar;
    this.progress.setMax(this.fileCount);
    this.progress.setProgress(this.readedSize);
    Logger.e("setProgressBar", this.progress.getId());
  }

  public void setRun(boolean paramBoolean)
  {
    this.isRun = paramBoolean;
  }

  public void stopDownload()
  {
    this.isCancelDownload = true;
  }
}

/* Location:           C:\Documents and Settings\Administrator\classes_dex2jar.jar
 * Qualified Name:     com.appwoo.txtws.thread.BookStoreDownloadTask
 * JD-Core Version:    0.6.2
 */